summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/StringUtils.cpp57
-rw-r--r--src/StringUtils.h51
2 files changed, 59 insertions, 49 deletions
diff --git a/src/StringUtils.cpp b/src/StringUtils.cpp
index 5febf5d6c..a63525356 100644
--- a/src/StringUtils.cpp
+++ b/src/StringUtils.cpp
@@ -23,7 +23,7 @@ AString & AppendVPrintf(AString & str, const char * format, va_list args)
ASSERT(format != nullptr);
char buffer[2048];
- size_t len;
+ int len;
#ifdef va_copy
va_list argsCopy;
va_copy(argsCopy, args);
@@ -34,14 +34,14 @@ AString & AppendVPrintf(AString & str, const char * format, va_list args)
// MS CRT provides secure printf that doesn't behave like in the C99 standard
if ((len = _vsnprintf_s(buffer, ARRAYCOUNT(buffer), _TRUNCATE, format, argsCopy)) != -1)
#else // _MSC_VER
- if ((len = vsnprintf(buffer, ARRAYCOUNT(buffer), format, argsCopy)) < ARRAYCOUNT(buffer))
+ if ((len = vsnprintf(buffer, ARRAYCOUNT(buffer), format, argsCopy)) < static_cast<int>(ARRAYCOUNT(buffer)))
#endif // else _MSC_VER
{
// The result did fit into the static buffer
#ifdef va_copy
va_end(argsCopy);
#endif
- str.append(buffer, len);
+ str.append(buffer, static_cast<size_t>(len));
return str;
}
#ifdef va_copy
@@ -51,7 +51,6 @@ AString & AppendVPrintf(AString & str, const char * format, va_list args)
// The result did not fit into the static buffer, use a dynamic buffer:
#ifdef _MSC_VER
// for MS CRT, we need to calculate the result length
- // MS doesn't have va_copy() and does nod need it at all
len = _vscprintf(format, args);
if (len == -1)
{
@@ -63,11 +62,11 @@ AString & AppendVPrintf(AString & str, const char * format, va_list args)
#ifdef va_copy
va_copy(argsCopy, args);
#endif
- std::vector<char> Buffer(len + 1);
+ std::vector<char> Buffer(static_cast<size_t>(len) + 1);
#ifdef _MSC_VER
- vsprintf_s((char *)&(Buffer.front()), Buffer.size(), format, argsCopy);
+ vsprintf_s(&(Buffer.front()), Buffer.size(), format, argsCopy);
#else // _MSC_VER
- vsnprintf((char *)&(Buffer.front()), Buffer.size(), format, argsCopy);
+ vsnprintf(&(Buffer.front()), Buffer.size(), format, argsCopy);
#endif // else _MSC_VER
str.append(&(Buffer.front()), Buffer.size() - 1);
#ifdef va_copy
@@ -85,7 +84,7 @@ AString & Printf(AString & str, const char * format, ...)
str.clear();
va_list args;
va_start(args, format);
- std::string &retval = AppendVPrintf(str, format, args);
+ std::string & retval = AppendVPrintf(str, format, args);
va_end(args);
return retval;
}
@@ -108,11 +107,11 @@ AString Printf(const char * format, ...)
-AString & AppendPrintf(AString &str, const char * format, ...)
+AString & AppendPrintf(AString & dst, const char * format, ...)
{
va_list args;
va_start(args, format);
- std::string &retval = AppendVPrintf(str, format, args);
+ std::string & retval = AppendVPrintf(dst, format, args);
va_end(args);
return retval;
}
@@ -297,7 +296,6 @@ void ReplaceString(AString & iHayStack, const AString & iNeedle, const AString &
-// Converts a stream of BE shorts into UTF-8 string; returns a ref to a_UTF8
AString & RawBEToUTF8(const char * a_RawData, size_t a_NumShorts, AString & a_UTF8)
{
a_UTF8.clear();
@@ -314,22 +312,22 @@ AString & RawBEToUTF8(const char * a_RawData, size_t a_NumShorts, AString & a_UT
a_UTF8.push_back((char)(192 + c / 64));
a_UTF8.push_back((char)(128 + c % 64));
}
- else if (c - 0xd800u < 0x800)
+ else if (c - 0xd800 < 0x800)
{
// Error, silently drop
}
else if (c < 0x10000)
{
- a_UTF8.push_back((char)(224 + c / 4096));
- a_UTF8.push_back((char)(128 + c / 64 % 64));
- a_UTF8.push_back((char)(128 + c % 64));
+ a_UTF8.push_back(static_cast<char>(224 + c / 4096));
+ a_UTF8.push_back(static_cast<char>(128 + (c / 64) % 64));
+ a_UTF8.push_back(static_cast<char>(128 + c % 64));
}
else if (c < 0x110000)
{
- a_UTF8.push_back((char)(240 + c / 262144));
- a_UTF8.push_back((char)(128 + c / 4096 % 64));
- a_UTF8.push_back((char)(128 + c / 64 % 64));
- a_UTF8.push_back((char)(128 + c % 64));
+ a_UTF8.push_back(static_cast<char>(240 + c / 262144));
+ a_UTF8.push_back(static_cast<char>(128 + (c / 4096) % 64));
+ a_UTF8.push_back(static_cast<char>(128 + (c / 64) % 64));
+ a_UTF8.push_back(static_cast<char>(128 + c % 64));
}
else
{
@@ -382,7 +380,7 @@ Notice from the original file:
-static const char trailingBytesForUTF8[256] =
+static const Byte trailingBytesForUTF8[256] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@@ -572,18 +570,18 @@ AString & CreateHexDump(AString & a_Out, const void * a_Data, size_t a_Size, siz
int Count = sprintf(line, "%08x:", (unsigned)i);
#endif
// Remove the terminating nullptr / leftover garbage in line, after the sprintf-ed value
- memset(line + Count, 32, sizeof(line) - Count);
+ memset(line + Count, 32, sizeof(line) - static_cast<size_t>(Count));
p = line + 10;
q = p + 2 + a_BytesPerLine * 3 + 1;
for (size_t j = 0; j < k; j++)
{
- unsigned char c = ((unsigned char *)a_Data)[i + j];
+ Byte c = (reinterpret_cast<const Byte *>(a_Data))[i + j];
p[0] = HEX(c >> 4);
p[1] = HEX(c & 0xf);
p[2] = ' ';
if (c >= ' ')
{
- q[0] = (char)c;
+ q[0] = static_cast<char>(c);
}
else
{
@@ -708,7 +706,7 @@ AString URLDecode(const AString & a_String)
res.push_back(ch);
continue;
}
- res.push_back((hi << 4) | lo);
+ res.push_back(static_cast<char>((hi << 4) | lo));
i += 2;
} // for i - a_String[]
return res;
@@ -767,7 +765,8 @@ AString Base64Decode(const AString & a_Base64String)
{
AString res;
size_t i, len = a_Base64String.size();
- int o, c;
+ size_t o;
+ int c;
res.resize((len * 4) / 3 + 5, 0); // Approximate the upper bound on the result length
for (o = 0, i = 0; i < len; i++)
{
@@ -850,7 +849,7 @@ AString Base64Encode(const AString & a_Input)
short GetBEShort(const char * a_Mem)
{
const Byte * Bytes = (const Byte *)a_Mem;
- return (Bytes[0] << 8) | Bytes[1];
+ return static_cast<short>((Bytes[0] << 8) | Bytes[1]);
}
@@ -870,9 +869,9 @@ int GetBEInt(const char * a_Mem)
void SetBEInt(char * a_Mem, Int32 a_Value)
{
a_Mem[0] = a_Value >> 24;
- a_Mem[1] = (a_Value >> 16) & 0xff;
- a_Mem[2] = (a_Value >> 8) & 0xff;
- a_Mem[3] = a_Value & 0xff;
+ a_Mem[1] = static_cast<char>((a_Value >> 16) & 0xff);
+ a_Mem[2] = static_cast<char>((a_Value >> 8) & 0xff);
+ a_Mem[3] = static_cast<char>(a_Value & 0xff);
}
diff --git a/src/StringUtils.h b/src/StringUtils.h
index 159e8ecac..bfe2a41fa 100644
--- a/src/StringUtils.h
+++ b/src/StringUtils.h
@@ -21,31 +21,40 @@ typedef std::list<AString> AStringList;
-/** Add the formated string to the existing data in the string */
-extern AString & AppendVPrintf(AString & str, const char * format, va_list args) FORMATSTRING(2, 0);
+/** Add the formated string to the existing data in the string.
+Returns a_Dst. */
+extern AString & AppendVPrintf(AString & a_Dst, const char * format, va_list args) FORMATSTRING(2, 0);
-/// Output the formatted text into the string
-extern AString & Printf (AString & str, const char * format, ...) FORMATSTRING(2, 3);
+/** Output the formatted text into the string.
+Returns a_Dst. */
+extern AString & Printf (AString & a_Dst, const char * format, ...) FORMATSTRING(2, 3);
-/// Output the formatted text into string, return string by value
+/** Output the formatted text into string
+Returns the formatted string by value. */
extern AString Printf(const char * format, ...) FORMATSTRING(1, 2);
-/// Add the formatted string to the existing data in the string
-extern AString & AppendPrintf (AString & str, const char * format, ...) FORMATSTRING(2, 3);
+/** Add the formatted string to the existing data in the string.
+Returns a_Dst */
+extern AString & AppendPrintf (AString & a_Dst, const char * format, ...) FORMATSTRING(2, 3);
-/// Split the string at any of the listed delimiters, return as a stringvector
+/** Split the string at any of the listed delimiters.
+Return the splitted strings as a stringvector. */
extern AStringVector StringSplit(const AString & str, const AString & delim);
-/// Split the string at any of the listed delimiters and trim each value, return as a stringvector
+/** Split the string at any of the listed delimiters and trim each value.
+Returns the splitted strings as a stringvector. */
extern AStringVector StringSplitAndTrim(const AString & str, const AString & delim);
-/// Trime whitespace at both ends of the string
+/** Trims whitespace at both ends of the string.
+Returns a trimmed copy of the original string. */
extern AString TrimString(const AString & str); // tolua_export
-/// In-place string conversion to uppercase; returns the same string
+/** In-place string conversion to uppercase.
+Returns the same string object. */
extern AString & InPlaceUppercase(AString & s);
-/// In-place string conversion to lowercase; returns the same string
+/** In-place string conversion to lowercase.
+Returns the same string object. */
extern AString & InPlaceLowercase(AString & s);
/** Returns an upper-cased copy of the string */
@@ -54,28 +63,30 @@ extern AString StrToUpper(const AString & s);
/** Returns a lower-cased copy of the string */
extern AString StrToLower(const AString & s);
-/// Case-insensitive string comparison; returns 0 if the strings are the same
+/** Case-insensitive string comparison.
+Returns 0 if the strings are the same, <0 if s1 < s2 and >0 if s1 > s2. */
extern int NoCaseCompare(const AString & s1, const AString & s2); // tolua_export
-/// Case-insensitive string comparison that returns a rating of equal-ness between [0 - s1.length()]
+/** Case-insensitive string comparison that returns a rating of equal-ness between [0 - s1.length()]. */
extern size_t RateCompareString(const AString & s1, const AString & s2);
-/// Replaces *each* occurence of iNeedle in iHayStack with iReplaceWith
+/** Replaces *each* occurence of iNeedle in iHayStack with iReplaceWith */
extern void ReplaceString(AString & iHayStack, const AString & iNeedle, const AString & iReplaceWith); // tolua_export
-/// Converts a stream of BE shorts into UTF-8 string; returns a ref to a_UTF8
+/** Converts a stream of BE shorts into UTF-8 string; returns a_UTF8. */
extern AString & RawBEToUTF8(const char * a_RawData, size_t a_NumShorts, AString & a_UTF8);
-/// Converts a UTF-8 string into a UTF-16 BE string; returns a ref to a_UTF16
+/** Converts a UTF-8 string into a UTF-16 BE string. */
extern AString UTF8ToRawBEUTF16(const char * a_UTF8, size_t a_UTF8Length);
-/// Creates a nicely formatted HEX dump of the given memory block. Max a_BytesPerLine is 120
+/** Creates a nicely formatted HEX dump of the given memory block.
+Max a_BytesPerLine is 120. */
extern AString & CreateHexDump(AString & a_Out, const void * a_Data, size_t a_Size, size_t a_BytesPerLine);
-/// Returns a copy of a_Message with all quotes and backslashes escaped by a backslash
+/** Returns a copy of a_Message with all quotes and backslashes escaped by a backslash. */
extern AString EscapeString(const AString & a_Message); // tolua_export
-/// Removes all control codes used by MC for colors and styles
+/** Removes all control codes used by MC for colors and styles. */
extern AString StripColorCodes(const AString & a_Message); // tolua_export
/// URL-Decodes the given string, replacing all "%HH" into the correct characters. Invalid % sequences are left intact